package com.sky.service.impl;

import com.sky.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * sky-take-out
 * com.sky.service.impl
 *
 * @author Ablecisi
 * @version 1.0
 * 2025/3/30
 * 13:49
 */
@Service
public class ShopServiceImpl implements ShopService {
    private final RedisTemplate<String, String> redisTemplate;
    private static final String SHOP_STATUS_KEY = "SHOP_STATUS";

    @Autowired
    public ShopServiceImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 设置店铺状态
     *
     * @param status 店铺状态
     */
    @Override
    public void setStatus(Integer status) {
        redisTemplate.opsForValue().set(SHOP_STATUS_KEY, String.valueOf(status));
    }

    @Override
    public Integer getStatus() {
        String status = redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        if (status == null) return 1; // 默认营业中
        int statusInt;
        try {
            statusInt = Integer.parseInt(status);
        } catch (NumberFormatException e) {
            throw new RuntimeException(e);
        }
        return statusInt;
    }
}
